<?php
/**
 * 核心控制器类
 */
namespace Hi;

class Action {

    /**
     * View实例
     * @var View
     */
    protected $view;

    /**
     * Params实例
     * @var Params
     */
    protected $params;

    /**
     * Request实例
     * @var mixed
     */
    protected $_request;

    /**
     * Action Instance
     * @var Action
     */
    protected static $_instance;

    /**
     * 构造
     * Action constructor.
     */
    function __construct() {
        //1、实例化
        self::$_instance = $this;
        //2、View类实例化
        $this->view = new View();
        //3、参数处理类实例化
        $this->params = Params::getInstance();
        //4、请求处理类实例化
        $this->_request = Request::getInstance();

        /**不明觉厉**/
        if (method_exists($this, '_init')) {
            $this->_init();
        }
    }

    /**
     * 向View传入变量
     * @param $variable
     * @param $value
     */
    protected function assign($variable, $value) {
        $this->view->assign($variable,$value);
    }

    /**
     * 转至给定的控制器和动作
     * @param $action
     * @param null $controller
     * @param array|NULL $params
     * @throws Exceptions
     */
    final protected function _forward($action, $controller = null, array $params = NULL) {
        if ($controller !== null) {
            $this->_request->setControllerName($controller);
        }
        $this->_request->setActionName($action);

        App::getInstance()->dispatch();
    }

    public static function getInstance() {
        return self::$_instance;
    }
}